from flask import Blueprint, jsonify, request

from application.services.system.dict_type import DictTypeService
from application.utils.res import json_fail, json_success, page_success

# 创建蓝图
bp = Blueprint("dict_type", __name__, url_prefix="/dict/type")

@bp.get("/optionselect")
def get_dict_type_optionselect():
    """
    获取字典类型下拉列表
    """
    try:
        result = DictTypeService.get_dict_type_optionselect()
        return json_success(result)
    except Exception as e:
        return json_fail(f"查询失败: {str(e)}"), 500


@bp.post("")
def create_dict_type():
    """ 
    创建字典类型
    """
    try:
        # 获取请求数据
        data = request.get_json()
        
        if not data:
            return json_fail("请求数据不能为空"), 400
            
        result = DictTypeService.create_dict_type(data)
        return json_success(result, "创建成功", 200)
    except Exception as e:
        return json_fail(f"创建失败: {str(e)}"), 500



@bp.put("")
def update_dict_data():
    """ 
    更新字典数据
    """
    try:
        # 获取请求数据
        data = request.get_json()
        
        if not data:
            return json_fail("请求数据不能为空"), 400
        
        dict_id = data.get("dictId")
        if not dict_id:
            return json_fail("字典ID不能为空"), 400

        result = DictTypeService.update_dict_type(dict_id, data)
        if result:
            return json_success(result, "更新成功")
        else:
            return json_fail("字典值不存在"), 404
    except Exception as e:
        return json_fail(f"更新失败: {str(e)}"), 500